package gis.toolkit;


import gis.common.JsonUtils;
import gis.core.utils.HttpRequest;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.json.JSONObject;
import sun.net.www.http.HttpClient;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;



/**
 * Created by xiexb on 2018/1/7.
 */
public class Expert {
   /* */

    /**
     * @param args
     *//*
    public static void main(String[] args) {

    }*/
    public static void main(String args[]) {
        try {

            JSONObject obj = new JSONObject();
            obj.put("institute", "云南省林业科学院");
            obj.put("name", "陈鹏");
            //obj.put("kid", "646F39E1-B5AD-C7F1-ED05-56886F2DD9BA");

            System.out.println(obj);
            // 创建url资源
            URL url = new URL("http://expert.ckcest.cn/api/queryNameOrgArray");
            // 建立http连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置允许输出
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 设置不用缓存
            conn.setUseCaches(false);
            // 设置传递方式
            conn.setRequestMethod("POST");
            // 设置维持长连接
            conn.setRequestProperty("Connection", "Keep-Alive");
            // 设置文件字符集:
            conn.setRequestProperty("Charset", "UTF-8");
            //转换为字节数组
            byte[] data = (obj.toString()).getBytes();
            // 设置文件长度
            conn.setRequestProperty("Content-Length", String.valueOf(data.length));
            //RequestEntity entity = new StringRequestEntity(str, "text/html", "utf-8");
            // 设置文件类型:
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            // 开始连接请求
            conn.connect();
            OutputStream out = conn.getOutputStream();
            // 写入请求的字符串
            out.write((obj.toString()).getBytes());

            out.flush();
            out.close();

            // 请求返回的状态
            if (conn.getResponseCode() == 200) {
                System.out.println("连接成功");
                // 请求返回的数据

                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
                String line;
                String result = "";
                while ((line = in.readLine()) != null) {
                    result += line;
                }
                System.out.println(result);

            } else {
                System.out.println("no++");
            }

            String b = HttpConnectionUtil.sendPostRequest("http://expert.ckcest.cn/api/queryNameOrgArray", obj.toString());

            System.out.println(b);

        } catch (Exception e) {

        }

    }

    public static  void test()
    {

    }
}